//
//  OneGroupModel.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/3.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class OneGroupModel: NSObject {
    var gc_id = "-999"
    var gc_name = "-999"
    var grandson = [OneItemModel]()
   
    lazy var items : [[OneItemModel]] = {
        
        var arrayM = [[OneItemModel]]()
        
        var subArrayM : [OneItemModel]?
        
        for (index,value) in self.grandson.enumerated() {
            if index % 3 == 0 {
                
                subArrayM = [OneItemModel]()
                
                subArrayM?.append(value)
                
                
            }else if index % 3 == 1 {
                
                subArrayM?.append(value)
                
//                arrayM.append(subArrayM!)
                
                
            }else{
            
                subArrayM?.append(value)
                
                arrayM.append(subArrayM!)
            
            }
            
            
        }
        
        
        if self.grandson.count % 3 == 1 {
            
            subArrayM = [OneItemModel]()
            
            subArrayM?.append(self.grandson.last!)
            
            arrayM.append(subArrayM!)
            
        }
        
        if self.grandson.count % 3 == 2 {
            
            subArrayM = [OneItemModel]()
            
            subArrayM?.append(self.grandson[self.grandson.count - 2])
            
            subArrayM?.append(self.grandson.last!)

            arrayM.append(subArrayM!)
            
        }
        
        
        return arrayM
        
        
    }()

    
    
    
    
    
    
    init(dic : [String : AnyObject]) {
        super.init()
        
        setValuesForKeys(dic)
        
    }
    
    
    override func  setValue(_ value: Any?, forKey key: String) {
        
        if key == "grandson" {
            
            
            if let temp = value as? [[String : AnyObject]]{
                
                for dic in temp {
                    
                    grandson.append(OneItemModel(dic:dic ))
                }
                
                
            }
            
 
            
   
            
        } else {
            
            
            super.setValue(value, forKey: key)
        }
        
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
        
    }
    
    
    //在swift中想要打印对象要以下重写属性
    override var description: String{
        let keys = ["gc_id","gc_name","grandson"]
        let dic = dictionaryWithValues(forKeys: keys)
        return "\(dic)"
    }

}
